package com.boss.common.util;

import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.sys.config.ColumnConfig;
import com.boss.sys.utils.SysUtils;

import java.util.*;

import static com.boss.sys.utils.SysUtils.nonNullStr;

/**
 * @author: todd
 * @description:
 * @date: ${date} ${time}
 */
public class PageUtils {

  /**
   * 公用分页
   * @param params
   * @return
   * @throws AppRuntimeException
   */
  public static PaginationDTO buildPaginationDTO(Map<String, Object> params) throws AppRuntimeException {
    PaginationDTO pageObj = null;
    if (params.containsKey("offset") && params.containsKey("limit")) {
      pageObj = new PaginationDTO();
      int limit = Integer.parseInt(params.get("limit").toString());
      int offset = Integer.parseInt(params.get("offset").toString());
      pageObj.setRows(limit);
      if (offset == 0) {
        pageObj.setCurrpage(0);
      } else {
        pageObj.setCurrpage(offset / limit );
      }

    }
    return pageObj;
  }

  public static PaginationDTO buildPaginationDTO(String offset, String limit) throws AppRuntimeException {
    PaginationDTO pageObj = null;
    pageObj = new PaginationDTO();
    int size = Integer.parseInt(limit);
    int start = Integer.parseInt(offset);
    pageObj.setRows(size);
    if (start == 0) {
      pageObj.setCurrpage(0);
    } else {
      pageObj.setCurrpage(start / size);
    }
    return pageObj;
  }

  public static PaginationDTO buildPaginationDTOPage(String page, String limit) throws AppRuntimeException {
    PaginationDTO pageObj = new PaginationDTO();
    int size = Integer.parseInt(limit);
    int start = Integer.parseInt(page);
    pageObj.setRows(size);
    pageObj.setCurrpage(start);
    return pageObj;
  }

  /**
   *
   * @param list
   * @return
   */
  public static List<Map<String, Object>> buildTreeData(List list) {
    if (SysUtils.isNullOrEmpty(list) || list.size() == 1) {
      if (!list.isEmpty()) {
        Map<String, Object> m = (Map<String, Object>) list.get(0);
        if (!m.containsKey("text")) {
          m.put("text", m.get("code") + "-" + m.get("name"));
        }
        if (!m.containsKey(ColumnConfig.ID)) {
          m.put(ColumnConfig.ID, m.get(ColumnConfig.GUID));
        }
        m.put("isleaf", "1");
      }
      return list;
    }
    int size = list.size();
    Map<String, Integer> guidPos = new HashMap<String, Integer>();
    Map<String, List<Map<String, Object>>> superMap = new HashMap<String, List<Map<String, Object>>>();
    String guid = null, superguid = null;
    List<Map<String, Object>> childList = null;
    Map<String, Object> m = null;
    for (int i = 0; i < size; i++) {
      m = (Map<String, Object>) list.get(i);
      if (!m.containsKey("text")) {
        m.put("text", m.get("code") + "-" + m.get("name"));
      }
      //默认都是底级
      m.put("isleaf", "1");
      m.put("icon", "fa fa-file-text");
      guid = nonNullStr(m.get(ColumnConfig.GUID));
      superguid = nonNullStr(m.get(ColumnConfig.SUPERGUID), "0");

      if (!m.containsKey(ColumnConfig.ID)) {
        m.put(ColumnConfig.ID, guid);
      }
      guidPos.put(guid, i);
      if (superMap.containsKey(superguid)) {
        childList = superMap.get(superguid);
      } else {
        childList = new ArrayList<Map<String, Object>>();
        superMap.put(superguid, childList);
      }
      childList.add(m);
    }
    Set<String> keySet = superMap.keySet();
    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
    for (String key : keySet) {
      if (guidPos.containsKey(key)) {
        m = (Map<String, Object>) list.get(guidPos.get(key));
        m.put("isleaf", "0");
        m.put("icon", "fa fa-folder");
        m.put("children", superMap.get(key));
      } else {
        result.addAll(superMap.get(key));
      }
    }
    //节点状态
    Map<String, Boolean> state = new HashMap<String, Boolean>();
    state.put("opened", true);
    m = result.get(0);
    m.put("state", state);
    return result;
  }
}
